package com.gxy.learn.redis.service;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Description(Redis 对象)
 * @author: Gao xueyong
 * @Create at: 2022/2/15 下午16:51
 */
@Service
public class RedisObjectService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * set redis: string类型
     *
     * @param key   key
     * @param value value
     */
    public void setObject(String key, Object value) {
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }

    /**
     * set redis: string类型
     *
     * @param key
     * @param value
     * @param timeout 超时时间
     * @param unit    超时时间单位
     */
    public void setObject(String key, Object value, long timeout, TimeUnit unit) {
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, timeout, unit);
    }

    /**
     * get redis: string类型
     *
     * @param key key
     * @return
     */
    public String getString(String key) {
        Object obj = redisTemplate.opsForValue().get(key);
        return null == obj ? null : String.valueOf(obj);
    }

    /**
     * get redis: 类型
     *
     * @param key key
     * @return
     */
    public Object getObject(String key) {
        Object obj = redisTemplate.opsForValue().get(key);
        return obj;
    }


    /**
     * set redis: hash类型
     *
     * @param key      key
     * @param filedKey filedkey
     * @param value    value
     */
    public void setHash(String key, String filedKey, Object value) {
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put(key, filedKey, value);
    }

    /**
     * set redis: hash类型
     *
     * @param key key
     * @param mp  map
     */
    public void setHash(String key, Map<String, Object> mp) {
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.putAll(key, mp);
    }

    /**
     * get redis: hash类型
     *
     * @param key      key
     * @param filedkey filedkey
     * @return
     */
    public Object getHash(String key, String filedkey) {
        return  redisTemplate.opsForHash().get(key, filedkey);
    }

    /**
     * set redis:list类型
     *
     * @param key   key
     * @param value value
     * @return
     */
    public long setList(String key, Object value) {
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        return listOperations.leftPush(key, value);
    }

    /**
     * set redis:list类型
     *
     * @param key   key
     * @param value value
     * @return
     */
    public long setList(String key, Object... value) {
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        return listOperations.leftPushAll(key, value);
    }

    /**
     * get redis:list类型
     *
     * @param key   key
     * @param start start
     * @param end   end
     * @return
     */
    public List<Object> getList(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }


    /**
     * get redis:list类型
     *
     * @param key key
     * @return
     */
    public List<Object> getList(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }


    /**
     * set redis:set类型
     *
     * @param key   key
     * @param value value
     * @return
     */
    public long setSet(String key, Object... value) {
        SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
        return setOperations.add(key, value);
    }

    /**
     * set redis:set类型
     *
     * @param key   key
     * @param value value
     * @return
     */
    public long setSet(String key, Object value) {
        SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
        return setOperations.add(key, value);
    }

    /**
     * get redis:set类型
     *
     * @param key key
     * @return
     */
    public Set<Object> getSet(String key) {
        return redisTemplate.opsForSet().members(key);
    }
}
